package com.teamtgv.shareshark;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.Spinner;

import com.teamtgv.shareshark.result.Result;
import com.teamtgv.shareshark.result.ResultComparator;

/**
 * 
 * This result list expects data passed via
 * myIntent.putParcelableArrayListExtra(ResultListActivity.RESULT_LIST,
 * resultList);
 * 
 */
public class ResultListActivity extends Activity {

	public static final String RESULT_LIST = "result_list";
	private List<Result> resultList;
	private ResultListAdapter adapter;
	private ListView list;
	private Spinner filterBy;
	private Spinner sortBy;
	
	private enum FILTER_METHOD {
		to_me, by_friends, all;
	}
	
	private enum SORTING_METHOD {
		date, distance, rating
	}
	
	// Default filter and sorting method
	private FILTER_METHOD filterMethod = FILTER_METHOD.all;
	private SORTING_METHOD sortMethod = SORTING_METHOD.date;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// hide title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_result_list);

		// Get results from intent data
		resultList = getIntent().getParcelableArrayListExtra(RESULT_LIST);

		// register list and dropdowns
		list = (ListView) ResultListActivity.this.findViewById(R.id.listResults);
		filterBy = (Spinner) this.findViewById(R.id.spinnerFilterBy);
		sortBy = (Spinner) this.findViewById(R.id.spinnerSortBy);

		// add listeners
		addListenersToDropDowns();
		addListenerToList();
		
		// if no result, display text
		if (resultList == null || resultList.isEmpty()) {
			list.setVisibility(View.INVISIBLE);
			filterBy.setVisibility(View.INVISIBLE);
			sortBy.setVisibility(View.INVISIBLE);
		// if result, hide "no results" text and show list
		} else {
			ResultListActivity.this.findViewById(R.id.textEmptyResultList).setVisibility(View.INVISIBLE);
			adapter = new ResultListAdapter(this, resultList);
			list.setAdapter(adapter);
			displayList(filterMethod, sortMethod);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.result_list, menu);

		return false;
	}
	
	// display the list of resultList with the selected sorting methods
	private void displayList(FILTER_METHOD filterBy, SORTING_METHOD sortBy) {
		Log.v("RESULT LIST", "Displaying result list, filtered by \"" + filterBy.name() + "\", sorted by \"" + sortBy.name() + "\"");
		this.filterMethod = filterBy;
		this.sortMethod = sortBy;
		
		filterList(filterBy);
		sortList(adapter, sortBy);
		
		adapter.notifyDataSetChanged();
	}
	
	private void filterList(FILTER_METHOD method) {
		List<Result> filteredResultList = new ArrayList<Result>();
		for (Result result : resultList) {
			if (method == FILTER_METHOD.by_friends && ! UserData.getFriendList().contains(result.getRecommender()))
				continue;
			if (method == FILTER_METHOD.to_me && ! UserData.getUsername().equalsIgnoreCase(result.getTarget()))
				continue;
			filteredResultList.add(result);
		}
		adapter = new ResultListAdapter(this, filteredResultList);
		list.setAdapter(adapter);
	}
	 
	/**
	 * sort list according to identified method 
	 * @param adapter
	 * @param method
	 */
	private void sortList(ResultListAdapter adapter, SORTING_METHOD method) {
		if (method == SORTING_METHOD.date)
			adapter.sort(ResultComparator.getDateComparator());
		
		if (method == SORTING_METHOD.distance)
			adapter.sort(ResultComparator.getDistanceComparator());
		
		if (method == SORTING_METHOD.rating)
			adapter.sort(ResultComparator.getRatingComparator());
	}

	// add the click listener to the list
	private void addListenerToList() {
		final ListView list = (ListView) this.findViewById(R.id.listResults);
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position,	long id) {
				// Get item from the list
				Result item = (Result) list.getAdapter().getItem(position);
				// Create intent for the Result View Activity
				Intent myIntent = new Intent(ResultListActivity.this, ResultViewActivity.class);
				// Pass the result as data
				myIntent.putExtra(ResultViewActivity.RESULT_OBJECT, item);
				ResultListActivity.this.startActivity(myIntent);
			}
		});
	}
	
	// add listeners to the dropdowns
	private void addListenersToDropDowns() {
			
		filterBy.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				Log.v("ResultListActivity", "Listener for dropdown");
				if (position == 0)
					displayList(FILTER_METHOD.all, sortMethod);
				if (position == 1)
					displayList(FILTER_METHOD.by_friends, sortMethod);
				if (position == 2)
					displayList(FILTER_METHOD.to_me, sortMethod);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
        sortBy.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				if (position == 0)
					displayList(filterMethod, SORTING_METHOD.date);
				//if (position == 1)
					//displayList(filterMethod, SORTING_METHOD.distance);
				if (position == 1)
					displayList(filterMethod, SORTING_METHOD.rating);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}
	
	@Override
	public void onResume() {
		super.onResume();
		displayList(filterMethod, sortMethod);
	}
}
